<?php namespace system\model;
use houdunwang\model\Model;
use houdunwang\session\Session;

class User extends Model{
	//数据表
	protected $table = "user";

	//允许填充字段
	protected $allowFill = [ '*'];

	//禁止填充字段
	protected $denyFill = [ ];

	//自动验证
	protected $validate=[
		//['字段名','验证方法','提示信息',验证条件,验证时间]
		['user_name', 'isnull', '用户名不能为空', self::MUST_VALIDATE],
		['user_password', 'isnull', '密码不能为空', self::MUST_VALIDATE],
		['code', 'isnull', '验证码不能为空', self::MUST_VALIDATE]
	];

	//自动完成
	protected $auto=[
		//['字段名','处理方法','方法类型',验证条件,验证时机]
	];

	//自动过滤
    protected $filter=[
        //[表单字段名,过滤条件,处理时间]
    ];

	//时间操作,需要表中存在created_at,updated_at字段
	protected $timestamps=true;
	//登录
	public function login()
	{
		//2.比对post提交是否与数据一致
		$userInfo = $this->where('user_name', $_POST['user_name'])->where('user_password', Crypt::encrypt($_POST['user_password']))->first();
		if (!$userInfo) {
			//用户名密码不对
			return ['valid' => 0, 'msg' => '用户不存在或密码不正确'];
		}
		//1.比对验证码
		if(strtolower(Code::get())!=strtolower($_POST['code']))
		{
			return ['valid'=>0,'msg'=>'请输入正确的验证码'];
		}
		//将用户信息存入session
		$_SESSION['username'] = $userInfo['user_name'];
//		p($_SESSION);die;
		return ['valid'=>1,'msg'=>'登录成功'];

	}
	//前台注册
	public function reg()
	{
		//1.比对验证码
		if(strtolower(Code::get())!=strtolower($_POST['code']))
		{
			return ['valid'=>0,'msg'=>'请输入正确的验证码'];

		}
		return ['valid'=>1,'msg'=>'注册成功'];
	}
	//
}